package physics;
import java.awt.Color;
import java.awt.Graphics2D;

/**
 * This class establishes the behavior of the muscle connector,
 * specifically its changing rest length.
 * @author Sam Rang
 */
public class Muscle extends Connector {
    private static final double FREQ = .01;
    private double myAmplitude;
    private int myTimer;
    private double myStartLength;

    /**
     * Establishes the Amplitude and sinusoidial functionality of the muscle.
     * @param start The mass at one end
     * @param end The mass at the other end
     * @param length The rest length of the muscle
     * @param kVal The spring constant
     * @param amp The amplitude of the oscillation
     */
    public Muscle (Mass start, Mass end,
            double length, double kVal, double amp) {
        super(start, end, length, kVal);
        myStartLength = length;
        myAmplitude = amp;
        myTimer = 0;
    }

    /**
     * Using the FREQ and myTimer variables, this method creates a
     * sinusoid that changes the rest length of the muscle so it oscillates.
     * @param dx The x-length
     * @param dy The y-length
     */
    @Override
    public Force setForce (double dx, double dy) {
        myTimer += 1;
        double leng = myAmplitude * Math.sin(FREQ * myTimer);
        setLength(myStartLength + leng);
        Force f = new Force(Force.angleBetween(dx, dy), getK()
                * (getLength() - Force.distanceBetween(dx, dy)));
        return f;
    }

    @Override
    public void checkStyle (double len, double restLength, Graphics2D pen) {
        if (Math.sin(FREQ * myTimer) > 0) {
            pen.setColor(Color.GREEN);
        }
        else {
            pen.setColor(Color.ORANGE);
        }
    }

}
